//获取应用实例

const app = getApp()
Page({
  data: {
    transportValues: ["收货时间不限", "周六日/节假日收货", "周一至周五收货"],
    transportIndex: 0,
    region: [app.globalData.province, app.globalData.city, app.globalData.county],
    // region: ['上海市', '上海市', '闵行区'],
    switch1Checked:1,
    list:[],
  },

  /**
    * 生命周期函数--监听页面加载
    */
  onLoad: function (e) {
    var that = this;
    console.log(app.globalData.province)
    that.setData({
      region: [app.globalData.province, app.globalData.city, app.globalData.county],
    })
  },
  //选择 地区
  bindRegionChange: function (e) {
    console.log(e.detail.value)
    console.log(e.detail.value[0], e.detail.value[1], e.detail.value[2])
    this.setData({
      region: e.detail.value
    })
  },
  //选择 收货时间
  bindTransportDayChange: function (e) {
    console.log('picker country 发生选择改变，携带值为', e.detail.value);
    this.setData({
      transportIndex: e.detail.value
    })
  },
  //保存地址 返回上一页
  //验证
  bindSave: function (e) {
    var that = this;
    var addressee = e.detail.value.addressee;
    var addresseePhone = e.detail.value.addresseePhone;
    var shipAddress = e.detail.value.shipAddress;
    if (addressee == "") {
      wx.showModal({
        title: '提示',
        content: '请填写联系人姓名',
        showCancel: false
      })
      return
    }
    if (addresseePhone == "") {
      wx.showModal({
        title: '提示',
        content: '请填写手机号码',
        showCancel: false
      })
      return
    }

    if (shipAddress == "") {
      wx.showModal({
        title: '提示',
        content: '请填写详细地址',
        showCancel: false
      })
      return
    }
    console.log(addressee, addresseePhone, this.data.region, shipAddress, this.data.switch1Checked)
    wx.request({
      url: app.globalData.the_host + `/shopping/shipaddress`,// + app.globalData.openId,
      data: {
        "openId": app.globalData.openId,
        "addressee": addressee,
        "addresseePhone": addresseePhone,
        "province": this.data.region[0],
        "city": this.data.region[1],
        "district": this.data.region[2],
        "shipAddress": shipAddress,
        "isDefault": this.data.switch1Checked
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        if(res.data.status==0){
         // wx.navigateBack({})
          if (that.data.switch1Checked==1){
           that.datelist(res.data.data);//如果switch1Checked ==1是默认地址，那么执行批量修改方法
         }else{
            wx.navigateBack({})
         }
         
        }else{
          wx.showToast({
            title: `添加失败`,
            icon: 'success',
            duration: 2000
          })
        }
      },
    })
  },
  /**
     * 获取地址
     */
  /* 使用全局 request 请求方法 app.js */
  datelist: function (id) {
    var that = this;
    app.request({
      "url": '/shopping/shipaddresses',      // 请求地址，必需的
      "method": "GET",     // 提交方式，默认POST,默认时可省略
      "data": { "openid": app.globalData.openId }    // 提交数据，默认undefined,不需要提交数据时可省略
    }, (res) => {
      //请求成功的回调函数
      that.setData({
        list: res,
      });
      if(that.data.list.length>1){
        that.updateAddress(id);//修改 非此id的数据
      }else{
        wx.navigateBack({})
      }
    }, () => {
      //请求失败的回调函数，不需要时可省略
    })
  },
  //批量修改默认地址
  updateAddress:function(id){
    var that = this;
    for (let i = 0; i < that.data.list.length; i++) {
      if (that.data.list[i].id!=id){
        app.request({
          "url": '/shopping/shipaddress',      // 请求地址，必需的
          "method": "PUT",     // 提交方式，默认POST,默认时可省略
          "data": { 
            "id": that.data.list[i].id,
            "openId": that.data.list[i].openId, //app.globalData.openId,
            "addressee": that.data.list[i].addressee,
            "addresseePhone": that.data.list[i].addresseePhone,
            "province": that.data.list[i].province,
            "city": that.data.list[i].opencityId,
            "district": that.data.list[i].district,
            "shipAddress": that.data.list[i].shipAddress,
            "isDefault": 0
           }    // 提交数据，默认undefined,不需要提交数据时可省略
        }, (res) => {
        })
      }
    }
    wx.navigateBack({})
   
 
  },
  //
  switch1Change:function(e){
    console.log('带值为', e.detail.value);
    if (e.detail.value){
      this.setData({
        switch1Checked: 1
      })
    }else{
      this.setData({
        switch1Checked: 0
      })
    }
    
  },
  bindCancel:function(){
    wx.navigateBack()
  }


})